package com.example.littleken.ormlite_text.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.littleken.ormlite_text.R;
import com.example.littleken.ormlite_text.Utils.Utils;
import com.example.littleken.ormlite_text.bean.LifeDiary;
import com.example.littleken.ormlite_text.db.DatabaseHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.UpdateBuilder;

import java.sql.SQLException;

public class UpdateActivity extends AppCompatActivity implements View.OnClickListener{
    private Button btn_update;
    private Button btn_goback;
    private EditText et_title_update;
    private EditText et_lifediary_update;
    //用于保存数据
    private Intent intent = null;
    private Bundle bundle = null;
    private LifeDiary lifediary = null;
    public static final String BUNDLE_MAIN_TO_UPDATE = "bundleformaintoupdate";

    //数据库连接
    public DatabaseHelper databaseHelper = null;
    public Dao lifediaryDAO = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update);
        /*initDb();*/
        //初始化数据库连接
        lifediaryDAO= Utils.initDb(UpdateActivity.this);
        //获取需要修改的日记实体类
        intent = getIntent();
        bundle = intent.getExtras();
        lifediary = (LifeDiary) bundle.getSerializable(BUNDLE_MAIN_TO_UPDATE);
        initView();
    }

    private void initView() {
        btn_update = (Button) findViewById(R.id.btn_update);
        btn_goback = (Button) findViewById(R.id.btn_goback);
        et_title_update = (EditText) findViewById(R.id.et_title_update);
        et_lifediary_update = (EditText) findViewById(R.id.et_lifediary_update);
        btn_update.setOnClickListener(this);
        btn_goback.setOnClickListener(this);

        et_title_update.setText(lifediary.getTitle());
        et_lifediary_update.setText(lifediary.getMessage());
    }

    @Override
    public void onClick(View view) {
        try{
            switch (view.getId()){
                case R.id.btn_update:
                    update();//更新日记
                    break;
                case R.id.btn_goback:
                    goback();//撤销更新
                    break;
            }
        }catch (Exception e){
            UpdateActivity.this.setResult(RESULT_CANCELED);//返回更新失败消息
            e.printStackTrace();
        }

    }

    /**
     * 更新日记
     * @throws SQLException 数据库异常
     */
    private void update() throws SQLException {
        //获取更新语句构造器
        UpdateBuilder update = lifediaryDAO.updateBuilder();
        String title = et_title_update.getText().toString().trim();
        String message = et_lifediary_update.getText().toString().trim();
        //输入框判空
        if(TextUtils.isEmpty(title) && TextUtils.isEmpty(message)){
            Toast.makeText(UpdateActivity.this,(String) this.getResources().getText(R.string.asklifediary),Toast.LENGTH_SHORT).show();
            return;
        }
        if(TextUtils.isEmpty(message)){
            Toast.makeText(UpdateActivity.this,(String) this.getResources().getText(R.string.lifediaryisenpty),Toast.LENGTH_SHORT).show();
            return;
        }
        //生成update语句
        update.setWhere(update.where().eq("id",lifediary.getId()));
        update.updateColumnValue("title",title);
        update.updateColumnValue("message",message);
        update.updateColumnValue("createtime",Utils.getNowTime());
        //更新
        update.update();
        //给MainActivity返回更新成消息
        UpdateActivity.this.setResult(RESULT_OK);
        //结束，跳转回MainActivity
        UpdateActivity.this.finish();
    }

    /**
     * 撤销更新，把记录恢复到修改前的状态
     */
    private void goback() {
        et_title_update.setText(lifediary.getTitle());
        et_lifediary_update.setText(lifediary.getMessage());
    }

}
